package edu.iyte.ceng431.hw2.pos.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Observable;

public class Sale extends Observable implements ISale {

    private int id;
    private Date date;
    private Register register;
    private Cashier cashier;
    private ArrayList<SaleLineItem> saleLineItems;
    private ArrayList<Payment> payments;

    public Sale(Register reg, Cashier cas) {
        this.register = reg;
        this.cashier = cas;
        this.date = new Date();
        this.saleLineItems = new ArrayList<SaleLineItem>();
        this.payments = new ArrayList<Payment>();
    }

    public Sale(int id, Date date) {
        this.id = id;
        this.date = new Date();
        this.saleLineItems = new ArrayList<SaleLineItem>();
        this.payments = new ArrayList<Payment>();
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public ArrayList<SaleLineItem> getSaleLineItems() {
        return saleLineItems;
    }

    public void setSaleLineItems(ArrayList<SaleLineItem> saleLineItems) {
        this.saleLineItems = saleLineItems;
    }

    @Override
    public double calculateTotal() {
        double total = 0;
        Iterator<SaleLineItem> it = saleLineItems.iterator();

        while (it.hasNext()) {
            total += it.next().calculateSum();
        }
        return total;
    }

    @Override
    public void addSaleLineItem(Product product, int quantity) {
        saleLineItems.add(new SaleLineItem( product, quantity));
        setChanged();
        notifyObservers();
    }
    
    public void addSaleLineItem(int id,Product product, int quantity) {
        saleLineItems.add(new SaleLineItem(id, product, quantity));
    }

    @Override
    public double calculatePayments() {
        double total = 0;

        Iterator<Payment> it = payments.iterator();
        while (it.hasNext()) {
            total += it.next().getAmount();
        }
        return total;
    }

    @Override
    public boolean saleComplete() {
        if (calculateTotal() != calculatePayments()) {
            return false;
        }
        return true;
    }

    public void setRegister(Register register) {
        this.register = register;
    }

    public Register getRegister() {
        return register;
    }

    public int getRegisterId() {
        return register.getId();
    }

    public void setCashier(Cashier cashier) {
        this.cashier = cashier;
    }

    public Cashier getCashier() {
        return cashier;
    }

    public int getCashierId() {
        return cashier.getId();
    }

    public void setPayments(ArrayList<Payment> payments) {
        this.payments = payments;
    }

    public ArrayList<Payment> getPayments() {
        return payments;
    }

    @Override
    public void addPayment(Payment pay) {
        this.payments.add(pay);

    }

    public double calculateCashTotal() {
        Iterator<Payment> it = this.payments.iterator();
        double total = 0;

        while (it.hasNext()) {
            Payment pay = it.next();
            if (pay instanceof Cash) {
                total += pay.getAmount();
            }
        }

        return total;
    }

    public double calculateCreditCardTotal() {
        Iterator<Payment> it = this.payments.iterator();
        double total = 0;

        while (it.hasNext()) {
            Payment pay = it.next();
            if (pay instanceof CreditCard) {
                total += pay.getAmount();
            }
        }

        return total;
    }
}
